﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using static PengSW.Languages.LanguageHelper;

namespace PengSW.InputHelper
{
    /// <summary>
    /// TextEdit.xaml 的交互逻辑
    /// </summary>
    public partial class TextEdit : Window, INotifyPropertyChanged
    {
        public TextEdit(string aInitialValue, string aTitle = "文本编辑")
        {
            InitializeComponent();
            // this.Title = aTitle;
            this.Title = string.Format(_T("STR004919"));
            Text = aInitialValue;
            this.DataContext = this;
        }

        public string Text { get { return _Text; } set { if (_Text == value) return; _Text = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Text))); } }
        private string _Text;

        private void OnImportFile_Click(object sender, RoutedEventArgs e)
        {
            OpenTextFileDialog aDlg = new OpenTextFileDialog();
            if (aDlg.ShowDialog() != true) return;
            try
            {
                Text = File.ReadAllText(aDlg.FileName, aDlg.CurrentEncoding);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void OnOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void OnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
